package com.mgy.agent;


import com.sun.tools.classfile.ClassReader;

import java.io.IOException;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.lang.instrument.Instrumentation;
import java.security.ProtectionDomain;

public class TraceAgent {

    public static void premain(String agentArgs, Instrumentation instrumentation) {

        System.out.println("agentArgs=" + agentArgs);
        instrumentation.addTransformer((ClassLoader loader, String className, Class<?> classBeingRedefined,
                                        ProtectionDomain protectionDomain, byte[] classfileBuffer) -> {
            if (loader != null) {
                System.out.println("className=" + className);
                System.out.println("loader=" + loader);
                System.out.println("classBeingRedefined=" + classBeingRedefined);
                System.out.println("protectionDomain=" + protectionDomain);
            }
            return classfileBuffer;
        }, true);
    }


    public static void agentmain(String agentArgs, Instrumentation instrumentation) {
        premain(agentArgs, instrumentation);
        //instrumentation.retransformClasses();
    }
}
